//
//  XMainViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XMainViewController.h"
#import "XNavigationViewController.h"
#import "XHomeViewController.h"
#import "XMineViewController.h"
#import "XTabBar.h"

#import "XActionSheetViewController.h"
#import "XRcordEditViewController.h"
#import "XBookkeepingEditViewController.h"

#import "RFaceCheckRequest.h"

@interface XMainViewController ()

@property (nonatomic, strong)XTabBar *xTabBar;

@end

@implementation XMainViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"floatActionNotification" object:nil];
}

- (void)loadView{
    [super loadView];
    
    self.xTabBar = [XTabBar new];
    
    //去除tabbar黑色线
    self.xTabBar.barStyle = UIBarStyleBlack;
    self.xTabBar.tintColor = COLOR_THEME_A;
    self.xTabBar.barTintColor = COLOR_WHITE;
    self.xTabBar.translucent = NO;
    
    //设置阴影
    self.xTabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.xTabBar.layer.shadowOffset = CGSizeMake(0, -1);
    self.xTabBar.layer.shadowOpacity = 0.3;
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *standardAppearance = [UITabBarAppearance new];
        [standardAppearance configureWithOpaqueBackground];
        standardAppearance.backgroundColor = COLOR_WHITE;
        self.xTabBar.standardAppearance = standardAppearance;
        if (@available(iOS 15.0, *)) {
            self.xTabBar.scrollEdgeAppearance = standardAppearance;
        }
    }
    
    [self setValue:self.xTabBar forKey:@"tabBar"];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadSubControllers];
    
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentPickerNoticition:) name:@"floatActionNotification" object:nil];
}

- (void)loadSubControllers {
    
    NSMutableArray* tabBarControllers = [NSMutableArray array];


    // 首页
    XHomeViewController* countController = [XHomeViewController new];
    XNavigationViewController* countNav = [[XNavigationViewController alloc] initWithRootViewController:countController];
    UITabBarItem* countItem = [[UITabBarItem alloc] initWithTitle:@"计划" image:[[UIImage imageNamed:@"x_tabar_home_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"x_tabar_home_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    countItem.tag = 1001;
    countNav.tabBarItem = countItem;
    [tabBarControllers addObject:countNav];

    //我的
    XMineViewController* mineController = [XMineViewController new];
    XNavigationViewController* mineNav = [[XNavigationViewController alloc] initWithRootViewController:mineController];

    UITabBarItem* mineItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[[UIImage imageNamed:@"x_tabar_mine_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"x_tabar_mine_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    mineItem.tag = 1002;
    mineNav.tabBarItem = mineItem;
    [tabBarControllers addObject:mineNav];

    [self setViewControllers:tabBarControllers animated:NO];
}

- (void)presentPickerNoticition:(NSNotification*)notify{
    XActionSheetViewController* picker = [XActionSheetViewController new];
    picker.block = ^(XDynamicType integer) {
        
        UINavigationController* currentNav = (UINavigationController*)self.selectedViewController;
        
        if(integer == XDynamicTypeRecord){
            XRcordEditViewController* editController = [XRcordEditViewController new];
            editController.hidesBottomBarWhenPushed = YES;
            editController.operation = EditOperation_Add;
            [currentNav pushViewController:editController animated:YES];
        }else if(integer == XDynamicTypeBookKeeping){
            XBookkeepingEditViewController* editController = [XBookkeepingEditViewController new];
            editController.hidesBottomBarWhenPushed = YES;
            [currentNav pushViewController:editController animated:YES];
        }
    };
    [picker presentOverViewController:self];
}


- (void)requestData{
    //getEtc 3-a 4-b
    RFaceCheckRequest* request = [RFaceCheckRequest new];
    [RequestOperation request:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(NSArray*  _Nonnull data) {
            if(data.count != 3){
                [[NSNotificationCenter defaultCenter] postNotificationName:kChangeNotification object:NULL userInfo:NULL];
            }
        }];
    }];
}
@end
